package com.huike.clues.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.huike.clues.domain.vo.AssignClueVo;
import com.huike.clues.service.ITbClueService;

import java.util.List;


public class ExcelListener extends AnalysisEventListener<AssignClueVo> {
    //最大缓存数量
    private static final int MAX=5;

    //缓存的数据
    private List<AssignClueVo> cachedDataList = ListUtils.newArrayListWithExpectedSize(MAX);

    public ITbClueService clueService;

    public ExcelListener(ITbClueService clueService) {
        this.clueService = clueService;
    }
    @Override
    public void invoke(AssignClueVo data, AnalysisContext context) {
        cachedDataList.add(data);
        if (cachedDataList.size()>= MAX){
            clueService.importData(cachedDataList);
            cachedDataList=ListUtils.newArrayListWithExpectedSize(MAX);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        clueService.importData(cachedDataList);
    }

}
